/*
 * @Descripttion: 
 * @version: 
 * @Author: LLP
 * @Date: 2019-12-11 21:26:36
 * @LastEditors: LLP
 * @LastEditTime: 2019-12-22 09:34:50
 */
import { userLogin, getNickname } from '@/api/userLogin' 
import { setsomeinfo } from '@/api/kycAttest'
import { getToken, setToken, removeToken } from '@/filter/auth'

const user = {
  state: {
    isLogin: false,
    nickname: '',
    someinfo: null,
    token: getToken()
  },
  mutations: {
    SET_ISLOGIN: (state, isLogin) => {
      state.isLogin = isLogin
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_SOMEINFO: (state, someinfo) => {
      state.someinfo = someinfo
    },
    SET_NICKNAME: (state, name) => {
      state.nickname = name
    }
  },
  actions: {
    orLogin: ({ commit }, isLogin) => {
      commit('SET_ISLOGIN', isLogin)
    },
    /**
     * @name: 用户登录
     * ----------------
     * @test: test font
     * @msg: 
     * @param {type} 
     * @return: 
     */
    userLogin: ({ commit }, userInfo) => {
      const { phone, password, email } = userInfo
      return new Promise((resolve, reject) => {
        userLogin({ phone: phone, password: password, email: email }).then(response => {
          const { data } = response
          commit('SET_TOKEN', data)
          setToken(data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    /**
     * [description]
     * 获取名称
     * -----------------
     * @Author   LLP
     * @DateTime 2019-12-25T16:47:27+0800
     * @param    {[type]}                 options.commit [description]
     * @return   {[type]}                                [description]
     */
    getNickname: ({commit}) => {
      return new Promise((resolve, reject) => {
        getNickname().then(response => {
          const { data } = response
          commit('SET_NICKNAME', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    /**
     * @name: 检查手机号邮箱资金密码KYC认证结果是否设置
     * ----------------------
     * @test: test font
     * @msg: 
     * @param {type} 
     * @return: 
     */
    setsomeinfo: ({ commit }) => {
      return new Promise((resolve, reject) => {
        setsomeinfo().then(response => {
          const { data } = response
          if (!data) {
            reject('Verification failed, please Login again.')
          }
          commit('SET_SOMEINFO', data)
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    /**
     * @name: 清除token
     * -----------------
     * @test: test font
     * @msg: 
     * @param {type} 
     * @return: 
     */
    resetToken({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },
  }
}

export default user
